<script>
	import auth from "common/js/auth.js";
	// #ifdef APP-PLUS
	// import APPUpdate from "@/js_sdk/APPUpdate/index";
	import APPUpdate from "@/uni_modules/uni-upgrade-center-app/utils/check-update";
	import push from "@/js_sdk/dc-push/push.js"
	import util from "@/common/js/util.js"
	import {
		androidPhoneLoginConfig,
		iosPhoneLoginConfig,
		H5PhoneLoginConfig
	} from "@/common/js/config.js"
	const shanYanSDKModule = uni.requireNativePlugin('CLSDK-ShanYanSDKModule')
	const platform = uni.getSystemInfoSync().platform
	// #endif
	export default {
		mixins: [auth],
		onLaunch: function(e) {
			// #ifdef H5
			//分享code
			if(e.query.code){
				uni.setStorageSync('shareCode', e.query.code);
			}
			// #endif
			// #ifdef APP-PLUS  
			//本机号码一键登录初始化
			this.phoneLoginInit()
			let agree = plus.runtime.isAgreePrivacy();
			console.log('agree',agree);
			if(!agree){
				uni.reLaunch({
					url:'/pages/member/protocol/index'
				})
				return
			}
			plus.runtime.setBadgeNumber(0);
			//异步开启通知
			plus.push.getClientInfoAsync(()=>{
				// console.log("ios start listenPush")
				util.listenPush();
			})
			APPUpdate();
			// #endif  

			// #ifdef H5
			if (uni.getSystemInfoSync().platform == "ios") {
				uni.setStorageSync("initUrl", location.href);
			}
			// #endif
		
		},
		onShow: function() {
			console.log('onShow')
			this.$store.dispatch("init");
			// 防止进程不被杀死情况,应用被覆盖
			this.$store.commit('setIsLogin',Boolean(uni.getStorageSync('token')));
			// #ifdef APP-PLUS
				const args= plus.runtime.arguments;  
			    if(args){  
					console.log("args",args)
					if(uni.getSystemInfoSync().platform=="android"){
						const _url=args.split("//")[1];
						if(_url){
							const argumentsData = _url&&_url.split('?')[1];
							let shareCode = this.$util.getUrlQueryString('code',argumentsData);
							console.log('shareCode', shareCode);
							if(shareCode){
								uni.setStorageSync('shareCode', shareCode);
							}
							uni.navigateTo({
								url:'/'+_url
							})
						}
					}
					else{
						const argumentsData =args.split('?')[1];
						if(argumentsData){
							let shareCode = this.$util.getUrlQueryString('code',argumentsData);
							console.log('shareCode', shareCode);
							if(shareCode){
								uni.setStorageSync('shareCode', shareCode);
							}
							const _url= this.$util.getUrlQueryString('url',argumentsData);
							const magicBoxId= this.$util.getUrlQueryString('magicBoxId',argumentsData);
							if(_url&&magicBoxId){
								uni.navigateTo({
									url:`/${_url}?magicBoxId=${magicBoxId}`
								})
							}
						}
						
					}
					
			    }  
			// #endif
			// 自动登录
			// if (
			// 	!uni.getStorageSync("token") &&
			// 	!uni.getStorageSync("loginLock") &&
			// 	!uni.getStorageSync("unbound")
			// ) {
			// 	// #ifdef H5
			// 	if (this.$util.isWeiXin()) {
			// 		this.$util.getUrlCode((urlParams) => {
			// 			if (urlParams.source_member)
			// 				uni.setStorageSync("source_member", urlParams.source_member);
			// 			if (urlParams.code == undefined) {
			// 				this.$api.sendRequest({
			// 					url: "/wechat/api/wechat/authcode",
			// 					data: {
			// 						redirect_url: location.href,
			// 					},
			// 					success: (res) => {
			// 						if (res.code >= 0) {
			// 							location.href = res.data;
			// 						}
			// 					},
			// 				});
			// 			} else {
			// 				this.$api.sendRequest({
			// 					url: "/wechat/api/wechat/authcodetoopenid",
			// 					data: {
			// 						code: urlParams.code,
			// 					},
			// 					success: (res) => {
			// 						if (res.code >= 0) {
			// 							let data = {};
			// 							if (res.data.openid) data.wx_openid = res.data.openid;
			// 							if (res.data.unionid) data.wx_unionid = res.data.unionid;
			// 							if (res.data.userinfo) Object.assign(data, res.data
			// 								.userinfo);
			// 							this.authLogin(data);
			// 						}
			// 					},
			// 				});
			// 			}
			// 		});
			// 	}
			// 	// #endif
			// }
			const token = uni.getStorageSync('token');
			if (token) {
				this.$store.dispatch("getUserInfo");
			}
		},
		onHide: function() {},
		methods: {
			phoneLoginInit() {
				if (platform == "ios") {
					this.init(iosPhoneLoginConfig)
				} else {
					this.init(androidPhoneLoginConfig)
				}
			},
			init(config) {
				console.log("手机号一键登录init")
				shanYanSDKModule.init({
					appid: config.AppID,
				}, result => {
					console.log("shanYanSDKModule.init result is", result)
					if(platform=="ios"){
						if (result.code == 1000) {
							this.preload()
						} else {
							uni.showToast({
								icon: "error",
								title: "手机号一键登录初始化失败！"
							})
						}
					}
					else{
						if (result.code == 1022) {
							this.preload()
						} else {
							uni.showToast({
								icon: "error",
								title: "手机号一键登录初始化失败！"
							})
						}
					}
					//初始化回调
					// this.shanyan_code = JSON.stringify(result.code);
					// this.shanyan_result = JSON.stringify(result.result);
				});
			},
			//手机号一键登录预取
			preload() {
				console.log("手机号一键登录预取")
				if (platform == "ios") {
					shanYanSDKModule.preGetPhonenumber((complete) => {
						console.log(JSON.stringify(complete));
					});
				} else {
					shanYanSDKModule.getPhoneInfo((result) => {
						//预取号回调
						// this.shanyan_code = JSON.stringify(result.code);
						// this.shanyan_result = JSON.stringify(result.result);
						console.log("手机号一键登录预取 result is",result)
					});
				}
			},
			// 配置样式界面
			/**
			 * 授权登录
			 */
			authLogin(data) {
				uni.setStorage({
					key: "authInfo",
					data: data,
				});

				if (uni.getStorageSync("source_member"))
					data.source_member = uni.getStorageSync("source_member");

				this.$api.sendRequest({
					url: "/api/login/auth",
					data,
					success: (res) => {
						if (res.code >= 0) {
							uni.setStorage({
								key: "token",
								data: res.data.token,
								success: () => {
									this.$store.dispatch("getCartNumber");
									this.$store.commit("setToken", res.data.token);
								},
							});
						} else {
							uni.setStorage({
								key: "unbound",
								data: 1,
								success: () => {},
							});
						}
					},
				});
			},
		},
	};
</script>
<style lang="scss">
	/* #ifndef APP-NVUE */
	@import url("/common/css/iconfont.css");
	@import url("/common/css/iconfont1.css");
	@import "./common/css/main.scss";
	/* #endif */
</style>
